package stuttgart.uni.locationtracker;

import android.os.Bundle;
import android.os.RemoteException;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public  class FriendlistFragment extends ListFragment {
		/**
		 * The fragment argument representing the section number for this
		 * fragment.
		 */
		//Strange Bug if Variables are not static
		private static FriendList friendlist;
		private static String[] friends;
		private static String[] ids;
		private static ArrayAdapter<String> ar;

		public FriendlistFragment() {
		}
		
		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			super.onListItemClick(l, v, position, id);
			((LocationTrackerActivity)getActivity()).statusFragment.setFriendId(ids[position]);
			try {
				((LocationTrackerActivity)getActivity()).mService.requestFriendDataUpdate(ids[position]);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			getActivity().getActionBar().setSelectedNavigationItem(1);
		}

		public void updateFriendList(FriendList friendlist){
			System.out.println("Friendlist Fragment updateFriendList ar = "+ar);
			if(ar!=null){
				this.friendlist = friendlist;
				this.friends = friendlist.getUsername();
				this.ids = friendlist.getId();
				ar.clear();
				ar.addAll(friends);
				ar.notifyDataSetChanged();
			}
		}
		
		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			
			super.onActivityCreated(savedInstanceState);
			ar = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1);
			setListAdapter(ar);
			System.out.println("Friendlist Fragment onActivityCreate FriendlistFragment");
			
			System.out.println("Friendlist Fragment onActivityCreate ar = "+ar);
		}
	}